package com.tjian.fafa.user.module.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.module.home.result.HomeResult;
import com.tjian.fafa.user.util.image.ImageUtil;

import java.io.IOException;
import java.util.List;

public class CourierListAdapter extends BaseAdapter
{
    private List<HomeResult> mVector;
    private LayoutInflater mInflater;
    private Context mContext;

    //将传过来的对象，实例化为View类对象;
    public CourierListAdapter(Context context, List<HomeResult> vector)
    {
        if (vector == null)
        {
            return;
        }
        this.mVector = vector;
        mInflater = LayoutInflater.from(context);//LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
        mContext = context;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.home_item, null);
            holder = new ViewHolder();
            holder.photo = (View) convertView.findViewById(R.id.home_item_photo);
            holder.photo_avatar = (ImageView) holder.photo.findViewById(R.id.home_photo_item_avatar);
            holder.userNumber = (TextView) holder.photo.findViewById(R.id.user_ankuai_id_textview);
            holder.identityCardName = (TextView) holder.photo.findViewById(R.id.home_photo_item_name);
            holder.waitTaskStatus = (TextView) holder.photo.findViewById(R.id.waitTaskStatus_textViewId);
            holder.accumulative_delivery_quantity = (TextView) holder.photo.findViewById(R.id.accumulativeDeliveryQuantity_textViewId);
            holder.average_speed_of_delivery = (TextView) holder.photo.findViewById(R.id.averageDeliveryTime_textViewId);
            holder.applauseRate = (TextView) holder.photo.findViewById(R.id.applauseRate_textViewId);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        HomeResult result = mVector.get(position);
        ImageUtil imageUtil = new ImageUtil();
        holder.photo.setVisibility(View.GONE);
        holder.userNumber.setText("发发号：" + result.getUserNumber());
        holder.photo.setVisibility(View.VISIBLE);
        try
        {
            holder.photo_avatar.setImageBitmap(imageUtil.getAvatar(mContext, result.getAvatar()));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        holder.identityCardName.setText("姓名：" + result.getName());
        holder.waitTaskStatus.setText(result.getWaitTaskStatus());
        holder.accumulative_delivery_quantity.setText("累计送达数：" + result.getAccumulative_delivery_quantity());
        holder.average_speed_of_delivery.setText("平均送货时间：" + result.getAverage_speed_of_delivery() + "分钟");
        holder.applauseRate.setText("好评率：" + result.getApplauseRate() + "%");
        return convertView;
    }

    class ViewHolder
    {
        TextView userNumber;
        View photo;
        ImageView photo_avatar;
        TextView identityCardName;
        TextView waitTaskStatus;
        TextView accumulative_delivery_quantity;
        TextView average_speed_of_delivery;
        TextView applauseRate;
    }

    public int getCount()
    {
        return mVector.size();
    }

    public Object getItem(int position)
    {
        return mVector.get(position);
    }

    public long getItemId(int position)
    {
        return position;
    }

    public LayoutInflater getmInflater()
    {
        return mInflater;
    }

    public void setmInflater(LayoutInflater mInflater)
    {
        this.mInflater = mInflater;
    }
}